package easier.view.plate.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.king.keyboard.KingKeyboard;
import com.king.keyboard.KingKeyboard.KeyboardType;
import java.util.ArrayList;
import java.util.List;

public class PlateViewManager {

    /**
     * @param context   activity 提供window,展示键盘
     * @param view      plateView {@link R.layout#plate_view_fake_view}控制车牌号显示
     * @param container 键盘容器,决定显示位置
     */
    @SuppressLint( "ClickableViewAccessibility" )
    public PlateViewManager( Context context, View view, ViewGroup container ) {

        EditText editText = view.findViewById( R.id.actualEdit );

        TextView textView00 = view.findViewById( R.id.text00 );
        TextView textView01 = view.findViewById( R.id.text01 );
        TextView textView02 = view.findViewById( R.id.text02 );
        TextView textView03 = view.findViewById( R.id.text03 );
        TextView textView04 = view.findViewById( R.id.text04 );
        TextView textView05 = view.findViewById( R.id.text05 );
        TextView textView06 = view.findViewById( R.id.text06 );
        TextView textView07 = view.findViewById( R.id.text07 );

        View xny = view.findViewById( R.id.xny );

        ArrayList< TextView > list = new ArrayList<>();
        list.add( textView00 );
        list.add( textView01 );
        list.add( textView02 );
        list.add( textView03 );
        list.add( textView04 );
        list.add( textView05 );
        list.add( textView06 );
        list.add( textView07 );

        for ( int i = 0, listSize = list.size(); i < listSize; i++ ) {
            final int j = i + 1;
            TextView textView = list.get( i );
            textView.setOnTouchListener( new OnTouchListener() {
                @Override
                public boolean onTouch( View v, MotionEvent event ) {

                    int length = editText.getText().length();

                    if ( length <= 0 ) {
                        return false;
                    }

                    if ( j < length - 1 ) {
                        v.postDelayed( new Runnable() {
                            @Override
                            public void run() {

                                editText.setSelection( j );
                            }
                        }, 100 );
                    }
                    return false;
                }
            } );
        }

        editText.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged( CharSequence s, int start, int count, int after ) {

            }

            @Override
            public void onTextChanged( CharSequence s, int start, int before, int count ) {

            }

            @Override
            public void afterTextChanged( Editable s ) {

                for ( TextView textView : list ) {
                    textView.setText( "" );
                }

                for ( int i = 0; i < s.length(); i++ ) {

                    if ( i > 7 ) {
                        break;
                    }

                    TextView textView = list.get( i );
                    textView.setText( String.valueOf( s.charAt( i ) ) );
                }

                if ( s.length() > 7 ) {
                    xny.setVisibility( View.INVISIBLE );
                    textView07.setVisibility( View.VISIBLE );
                } else {
                    xny.setVisibility( View.VISIBLE );
                    textView07.setVisibility( View.INVISIBLE );
                }
            }
        } );

        KingKeyboard kingKeyboard = new KingKeyboard( context, (ViewGroup) view, container );
        kingKeyboard.register( editText, KeyboardType.LICENSE_PLATE );

        configEditText( editText );
        configTextView( list );
    }

    protected void configEditText( EditText editText ) {

        /* Android EditText屏蔽默认长按粘贴复制事件 */
        editText.setCustomSelectionActionModeCallback( new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode( ActionMode mode, Menu menu ) {

                return false;
            }

            @Override
            public boolean onPrepareActionMode( ActionMode mode, Menu menu ) {

                return false;
            }

            @Override
            public boolean onActionItemClicked( ActionMode mode, MenuItem item ) {

                return false;
            }

            @Override
            public void onDestroyActionMode( ActionMode mode ) {

            }
        } );
    }

    protected void configTextView( List< TextView > textViews ) {

    }

    /**
     * 验证str是否为正确的车牌号
     *
     * @return true
     */
    public static boolean isPlateNo( CharSequence no ) {

        if ( no == null || no.equals( "" ) ) {
            return false;
        }

        String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        String[] str1 = {"京", "津", "冀", "晋", "蒙", "辽", "吉", "黑", "沪", "苏",
            "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂", "琼", "渝",
            "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "农", "台", "中",
            "武", "WJ", "亥", "戌", "酉", "申", "未", "午", "巳", "辰", "卯", "寅",
            "丑", "子", "葵", "壬", "辛", "庚", "己", "戊", "丁", "丙", "乙", "甲",
            "河北", "山西", "北京", "北", "南", "兰", "沈", "济", "成", "广", "海", "空",
            "军", "京V", "使"};

        if ( no.equals( "新车" ) ) {
            return true;
        }

        if ( no.length() == 7 ) {
            int h = 0;
            for ( int r = 0; r < no.length(); r++ ) {
                if ( str.indexOf( no.charAt( r ) ) != -1 ) {
                    h++;
                }
            }
            if ( h == 7 ) {
                return true;
            }
        }

        if ( no.length() > 1 ) {
            String jq1 = no.subSequence( 0, 1 ).toString();
            String jq2 = no.subSequence( 0, 2 ).toString();
            for ( String s : str1 ) {
                if ( s.equals( jq1 ) ) {
                    if ( no.length() <= 8 ) {
                        return true;
                    }
                }
                if ( s.equals( jq2 ) ) {
                    if ( no.length() <= 8 ) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}
